using System;
using Loki.Bot.Logic;
using Loki.Bot.Logic.Bots.Grind;
using Loki.Utilities.Plugins;

namespace GrindZonesRandomizer
{
    /// <summary> </summary>
    public class GrindZonesRandomizer : IPlugin
    {
        public static Logging MyLogger = new Logging();
        public static GrinderSettings MySettings = new GrinderSettings();

        #region Implementation of IEquatable<IPlugin>

        /// <summary>
        ///     Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        ///     true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(IPlugin other)
        {
            return Name.Equals(other.Name);
        }

        #endregion

        #region Implementation of IPlugin

        /// <summary> </summary>
        public string Author
        {
            get { return "toNyx (and pushedx)"; }
        }

        /// <summary> </summary>
        public Version Version
        {
            get { return new Version(0, 1, 0, 0); }
        }

        /// <summary> </summary>
        public string Name
        {
            get { return "GrindZonesRandomizer"; }
        }

        /// <summary> </summary>
        public string Description
        {
            get { return "Grind more than 1 zone !"; }
        }

        /// <summary> Executes the start action. This is called when the bot starts. </summary>
        public void OnStart()
        {
            // Use our specific runetime 
            Registry.UserGetGrindZoneId = new MyGetGrindZoneId();
            Registry.DontUseSmartLedgeFarming = true;
        }

        /// <summary> Executes the stop action. This is called when the bot is stopped. </summary>
        public void OnStop()
        {
            // Restore the default settings.
            Registry.UserGetGrindZoneId = null;
            Registry.DontUseSmartLedgeFarming = false;
        }

        /// <summary> Executes the pulse action. This is called every "tick" of the bot. </summary>
        public void OnPulse()
        {
        }

        /// <summary>
        ///     Executes the initialize action. This is called at initial bot startup. (When the bot itself is started, not
        ///     when Start() is called)
        /// </summary>
        public void OnInitialize()
        {
        }

        /// <summary> Executes the shutdown action. This is called when the bot is shutting down. (Not when Stop() is called) </summary>
        public void OnShutdown()
        {
            // Save Settings before bot is shutting down
            MySettings.SaveSettings();
        }

        /// <summary> Executes the enabled action. This is called when the user has enabled this specific plugin via the GUI. </summary>
        public void OnEnabled()
        {
            MyLogger.Log("warn", "GrindZonesRandomizer is enabled");
            MySettings.LoadSettings();

            // Load last file used
            if(MySettings.LastZoneFilePath != null)
            {
                MyLogger.Log("warn", "Last zones pack file will now be loaded...");
                Helpers.MyZones.LoadZones(MySettings.LastZoneFilePath);
                
            }
                
        }

        /// <summary> Executes the disabled action. This is called whent he user has disabled this specific plugin via the GUI. </summary>
        public void OnDisabled()
        {
        }

        /// <summary> Executes the config action. This is called when the user clicks on the config button.</summary>
        public void OnConfig()
        {
            var myForm = new ConfigForm();
            myForm.Show();
        }

        #endregion

        #region Grinder

        /// <summary>
        /// An example grind zone changing plugin implementation. 
        /// </summary>
        internal class MyGetGrindZoneId : IGetGrindZoneId
        {
            private string _grindZoneId;

            private int _index;

            private bool _needToShuffle = true;

            public MyGetGrindZoneId()
            {
                _grindZoneId = Helpers.MyZones.Count > 0 ? string.Format("{0}_{1}", (int)Helpers.MyZones[_index].ZoneDifficulty, Helpers.MyZones[_index].ZoneID) : GrindBotSettings.Instance.GrindZoneId;
            }

            public string GetGrindZoneId()
            {
                return _grindZoneId;
            }

            public void DetermineNextGrindZone()
            {
                // Shuffling Attempt
                if (MySettings.RandomizeZones && _needToShuffle)
                {
                    Helpers.MyZones.Shuffle();
                    _needToShuffle = false;
                }

                // Determine Zone to grind
                if (_index <= Helpers.MyZones.Count)
                {
                    _grindZoneId = string.Format("{0}_{1}", (int) Helpers.MyZones[_index].ZoneDifficulty, Helpers.MyZones[_index].ZoneID);
                    _index++;
                }
                else
                {
                    _index = 0;
                    _needToShuffle = true;
                }
            }
        }

        #endregion Grinder
    }
}